<?php defined('SYSPATH') OR die('No direct access allowed.');

class ISS_Ajax {
	
	const EXCEPTION_EMPTY_SELECTOR_OR_CONTENT = 'Empty selector or content.';
	const EXCEPTION_EMPTY_TAGID_OR_SCRIPT = 'Empty tag id or script.';
	
	const UPDATE_VIEW = 'updateView';
	const SCRIPT_BLOCK = 'scriptBlock';
	
	private $view_array;
	private $exec_js_array;
	
	public function __construct() {
		
		// Initilize array
		$this->clearHtmlAjax();
	}
	
	/**
	 * This function will be return a collaction of view update and java script execution 
	 * with json encoding format used for send back to client.
	 * return Json encoded string                     
	 */
	public function getHtmlAjax() {
		$content = '';
		// Generate html content
		foreach ($this->view_array as $key=>$value) {
			$content_block = '';
			$size = strlen($value);
			$content_block .= $size;					// Content block size
			$content_block .= '|' . self::UPDATE_VIEW;	// Content block type
			$content_block .= '|' . $key;				// Selector block
			$content_block .= '|' . $value;				// Content block
			$content_block .= '|';						// End content block
			
			$content .= $content_block;
		}
		// Generate execute javascript
		foreach ($this->exec_js_array as $key=>$value) {
			$content_block = '';
			$size = strlen($value);
			$content_block .= $size;					// Content block size
			$content_block .= '|' . self::SCRIPT_BLOCK;	// Content block type
			$content_block .= '|' . $key;				// Selector block
			$content_block .= '|' . $value;				// Content block
			$content_block .= '|';						// End content block

			$content .= $content_block;
		}
		
		return $content;
	}
                
	/**
	 * This function will be clear all collection of view update and java script execution 
	 */
	public function clearHtmlAjax() {
		// Free array
		if (isset($this->view_array) && !empty($this->view_array)) 
			unset($this->view_array);
		if (isset($this->exec_js_array) && !empty($this->exec_js_array)) 
			unset($this->exec_js_array);
		// Reinitialize array
		$this->view_array = array();
		$this->exec_js_array = array();
	}
                
	/**
	 * Add content into list it's prepare for generate json encode 
	 * $selector The selector id of target want to update.
	 * $content The update content string will be replace on target.
	 */
	public function addViewUpdate($selector, $content) {
		if (!empty($selector) && !empty($content)) {
			$this->view_array[$selector] = $content;
		}
		else {
			throw new Exception(self::EXCEPTION_EMPTY_SELECTOR_OR_CONTENT);
		}
	}
                
	/**
	 * Add java script into list it's prepare for generate json encode and execute on client
	 * $tagid The reference id of this script.
	 * $script The java script string will be execute on client.
	 */
	public function addExecJScript($tagId, $script) {
		if (!empty($tagId) && !empty($script)) {
			$this->exec_js_array[$tagId] = $script;
		}
		else {
			throw new Exception(self::EXCEPTION_EMPTY_TAGID_OR_SCRIPT);
		}
	}
                
	/**
	 * Remove content from list thus it not generate json encode 
	 * $selector The selector id of content want to remove.
	 */
	public function removeViewUpdate($selector) {
		if (!empty($selector)) {
			unset($this->view_array[$selector]);
		}
		else {
			throw new Exception(self::EXCEPTION_EMPTY_SELECTOR_OR_CONTENT);
		}
	}

	/**
	 * Remove java script from list thus it not execute on client
	 * $tagId The reference id of this script.
	 */
	public function removeExecJScript($tagId) {
		if (!empty($tagId)) {
			unset($this->exec_js_array[$tagId]);
		}
		else {
			throw new Exception(self::EXCEPTION_EMPTY_TAGID_OR_SCRIPT);
		}
	}

}